package com.itcast.yygh.msm.service.impl;

import com.itcast.common.utils.RandomUtil;
import com.itcast.yygh.model.vo.msm.MsmVo;
import com.itcast.yygh.msm.service.MsmService;
import com.netflix.client.ClientException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.rmi.ServerException;
import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {
    /**
     * 发送短信
     */
    public boolean send(String PhoneNumbers, String templateCode, Map<String,Object> param) {

        if(StringUtils.isEmpty(PhoneNumbers)) return false;

       /* DefaultProfile profile =
                DefaultProfile.getProfile("default", "LTAIq6nIPY09VROj", "FQ7UcixT9wEqMv9F35nORPqKr8XkTF");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        request.putQueryParameter("PhoneNumbers", PhoneNumbers);
        request.putQueryParameter("SignName", "我的谷粒在线教育网站");
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }*/
        return true;
    }

    //发送短信实现
    @Override
    public boolean sendMsm(MsmVo msmVo) {
        if(!StringUtils.isEmpty(msmVo.getPhone())) {
            //String code = (String)msmVo.getParam().get("code");
            //仅为了测试
            String code = RandomUtil.getFourBitRandom();
            return this.send(msmVo.getPhone(),code,null);
        }
        return false;
    }
}
